<template>
    <div>
        <head-top></head-top>

        <div class="question table_container">

            <el-form :inline="true" :rules="rulesformQust" :model="formQust" class="demo-form-inline" ref="formQust">
                <el-form-item label="问卷类型" prop="qtype">
                    <el-select v-model="formQust.qtype" placeholder="请选择">
                        <el-option label="单选类型" value="1"></el-option>
                        <el-option label="复选类型" value="2"></el-option>
                        <el-option label="填写类型" value="3"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="启用状态" prop="expire">
                    <el-radio-group v-model="formQust.expire">
                        <el-radio label="1">全部</el-radio>
                        <el-radio label="2">启用</el-radio>
                        <el-radio label="3">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>

            <div class="but-group">
                <el-button  type="primary" @click="addqust">新增</el-button>
                <el-button  type="primary">搜索</el-button>
                <el-button  type="primary" @click="resetForm('formQust')">重置</el-button>
            </div>

            <el-table
                :data="tableData"
                style="width: 100%;margin-top: 20px">
                <el-table-column
                    prop="id"
                    label="序号"
                    width="180">
                </el-table-column>
                <el-table-column
                    prop="qtype"
                    label="问卷类型"
                    width="180">
                </el-table-column>
                <el-table-column
                    prop="qcontent"
                    label="问卷内容">
                </el-table-column>
                <el-table-column
                    prop="time"
                    label="有效期限">
                </el-table-column>
                <el-table-column
                    label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" size="small">启用</el-button>
                        <el-button type="text" size="small" @click="modifyqust(scope.$index, scope.row)">修改</el-button>
                        <el-button type="text" size="small" @click="handleDelete(scope.$index, scope.row, tableData)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="Pagination" style="text-align: left;margin-top: 10px;">
                <el-pagination
                    :current-page="currentPage"
                    :page-size="20"
                    layout="total, prev, pager, next"
                    :total="count">
                </el-pagination>
            </div>
        </div>
        <el-dialog :title="title" :visible.sync="dialogFormVisible">
            <el-form :inline="true" :rules="rulesform1" :model="form1" ref="form1" label-width="80px">
                <el-form-item label="问卷名称" prop="qname">
                    <el-input v-model="form1.qname" placeholder="请输入问卷名称"></el-input>
                </el-form-item>
                <el-form-item label="获赠积分" prop="qpoints">
                    <el-col :span="23">
                        <el-input v-model="form1.qpoints" placeholder="请输入积分"></el-input>
                    </el-col>
                    <el-col :span="1">M</el-col>
                </el-form-item>
                <el-form-item label="问卷类型" prop="qtype">
                    <el-select v-model="form1.qtype" placeholder="请选择" v-on:change="change()">
                        <el-option label="单选类型" value="1"></el-option>
                        <el-option label="复选类型" value="2"></el-option>
                        <el-option label="填写类型" value="3"></el-option>
                    </el-select>
                </el-form-item>
                <el-col :span="24">
                <el-form-item label="问卷内容" prop="qcontent">
                        <el-input type="textarea" v-model="form1.qcontent" placeholder="请输入内容" style="width: 450px"></el-input>
                </el-form-item>
                </el-col>
                <el-form-item label="有效期" required>
                    <el-col :span="11">
                        <el-form-item prop="date1">
                            <el-date-picker type="date" placeholder="选择日期" v-model="form1.date1" style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col class="line" :span="2">至</el-col>
                    <el-col :span="11">
                        <el-form-item prop="date2">
                            <el-date-picker type="date" placeholder="选择日期" v-model="form1.date2" style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-form-item>
                <el-col :span="24">
                <el-form-item label="附件图片" prop="qimg">
                    <el-upload
                        v-model="form1.qimg"
                        class="upload-demo"
                        action="https://jsonplaceholder.typicode.com/posts/"
                        multiple
                        :limit="3"
                        :file-list="fileList">
                        <el-button size="small" type="primary">附件上传</el-button>
                    </el-upload>
                </el-form-item>
                </el-col>
                <el-table
                    :data="formTData"
                    v-if="tableShow"
                    style="width: 100%">
                    <el-table-column
                        prop="id"
                        label="序号"
                        width="180">
                    </el-table-column>
                    <el-table-column
                        prop="name"
                        label="按钮名称"
                        width="180">
                        <template slot-scope="scope">
                            <el-input v-model="formTData.name" style="width: 100px"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="操作">
                        <template slot-scope="scope">
                            <el-button type="text" size="small" @click="handleDelete(scope.$index, scope.row, formTData)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false,resetForm('form1')">取消</el-button>
                <el-button type="primary" @click="resetForm('form1')">重置</el-button>
                <el-button type="primary" @click="submitForm('form1')">保存</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import headTop from '@/components/headTop'
    export default {
        data() {
            return {
                formQust: {
                    qtype: '',
                    expire: '1'
                },
                tableData: [
                    {
                        id: 1,
                        qname: '问卷1',
                        qpoints: '12',
                        qtype: '单选类型',
                        qcontent: '1519833600000',
                        time: ''
                    },{
                        id: 2,
                        qname: '问卷2',
                        qpoints: '5',
                        qtype: '复选类型',
                        qcontent: '1519833600000',
                        time: ''
                    },{
                        id: 3,
                        qname: '问卷3',
                        qpoints: '20',
                        qtype: '填写类型',
                        qcontent: '1519833600000',
                        time: ''
                    }],
                dialogFormVisible: false,
                form1: {
                    qname: '',
                    qpoints: '',
                    qtype: '',
                    qcontent: '',
                    date1: '',
                    date2: '',
                    qimg: ''
                },
                fileList: [],
                limit: 20,
                count: 3,
                currentPage: 1,
                title: '',
                rulesformQust: {
                    expire: [
                        { required: true, message: '请选中', trigger: 'change' }
                    ],
                    qtype: [
                        { required: true, message: '问卷类型不能为空', trigger: 'change' }
                    ]
                },
                rulesform1: {
                    qname: [
                        { required: true, message: '问卷名称不能为空', trigger: 'blur' }
                    ],
                    qpoints: [
                        { required: true, message: '获赠积分不能为空', trigger: 'blur' },
                        { type: 'number', message: '必须为数字值', trigger: 'blur' }
                    ],
                    qtype: [
                        { required: true, message: '请选择问卷类型', trigger: 'change' }
                    ],
                    date1: [
                        { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
                    ],
                    date2: [
                        { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
                    ],
                    qimg: [
                        { required: true, message: '请选择图片', trigger: 'change' }
                    ],
                    qcontent: [
                        { required: true, message: '请填写内容', trigger: 'blur' }
                    ]
                },
                tableShow: false,
                formTData: [{
                    id: '1',
                    name: '会'
                }, {
                    id: '2',
                    name: '不会'
                },{
                    id: '3',
                    name: '其他'
                }]
            }
        },
        components: {
            headTop
        },
        methods: {
//            重置
            resetForm(formName) {
                this.$refs[formName].resetFields('');
            },
            beforeRemove(file, fileList) {
                return this.$confirm(`确定移除 ${ file.name }？`);
            },
//            新增问卷
            addqust () {
                this.title = '新增问卷'
                this.dialogFormVisible = true
                this.form1 = []
            },
//            修改问卷
            modifyqust (index, row) {
                this.title = '修改问卷'
                this.dialogFormVisible = true
                this.form1 = Object.assign({}, row)
            },
//            删除表格行
            async handleDelete(index, row, data1) {
                this.$confirm('此操作将永久删除, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    data1.splice(index, 1)
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
//            保存
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        alert('submit!');
                    } else {
                        console.log('error submit!!')
                        return false;
                    }
                });
            },
            change(){
                if(this.form1.qtype == '3'){
                    this.tableShow = !this.tableShow
                }else {
                    this.tableShow = false
                }
            }
        }
    }
</script>

<style>
    .table_container{
        padding: 20px;
    }
</style>
